﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using System.Collections;
using System.Reflection;
using Model.Entity;

namespace LibrarySystem
{
    public partial class BorrowForm : Form
    {
        BorrowBLL bb = null;
        User user = null;
        Rfid rfid = null;
        DataTable borrowTable = null;

        public BorrowForm()
        {
            InitializeComponent();            
            bb = new BorrowBLL();
            borrowTable = new DataTable();
            NameList.DataSource = bb.getUser();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (userText.Text == "")
            {
                NameList.DataSource = bb.getUser();
            }
            else
            {
                NameList.DataSource = bb.getUser(int.Parse(userText.Text));
            }
        }        

        private void button2_Click(object sender, EventArgs e)
        {
            if (bookText.Text == "")
            {
                MessageBox.Show("请输入图书编号！");
            }
            else
            {
                rfid = bb.canBorrow(bookText.Text);
                if (rfid == null)
                {
                    MessageBox.Show("此图书已被借出，或未找到！");
                }
                else
                {
                    setBookInfo(rfid.book);
                }
            }
        }

        private void setBookInfo(Book book)
        {
            if (book != null)
            {
                L_bookName.Text = book.name;
                L_author.Text = book.author;
                L_id.Text = bookText.Text;
                if (book.publisher != null)
                {
                    L_publisher.Text = book.publisher.name;
                }
                if (book.bookType != null)
                {
                    L_type.Text = book.bookType.name;
                }
            }
            else
            {
                L_bookName.Text = null;
                L_author.Text = null;
                L_id.Text = null;
                L_publisher.Text = null;
                L_type.Text = null;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (user == null || rfid == null)
            {
                MessageBox.Show("请输入读者号和图书号后再借书！");
            }
            else
            {
                borrowTable.Merge(bb.borrowBook(user.id, rfid.rfid));
                DG_record.DataSource = borrowTable;
                MessageBox.Show("借书成功！");
                bookText.Text = "";
                setBookInfo(null);
            }
        }

        private void NameList_SelectionChanged(object sender, EventArgs e)
        {
            if (this.NameList.CurrentRow != null)
            {
                user = new User() { id = (int)this.NameList.CurrentRow.Cells["id"].Value };                
            }
        }
    }
}
